#import "IPhotoGallery.h"
#import "IPhotoGalleryDelegate.h"
#import "ThumbsViewController.h"
#import "SlideshowViewController.h"

@implementation IPhotoGallery

- (id)initWithDelegate:(id<IPhotoGalleryDelegate>)delegate
{
    thumbsViewController = [(ThumbsViewController *)[ThumbsViewController alloc]
                            initWithDelegate:self];
    slideshowViewController = [(SlideshowViewController *)
                               [SlideshowViewController alloc]
                               initWithDelegate:self];
    if (self = [super initWithRootViewController:thumbsViewController])
    {
        iPhotoGalleryDelegate = [delegate retain];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle
        = UIStatusBarStyleBlackTranslucent;
    self.navigationBar.translucent = YES;
    self.navigationBar.barStyle = UIBarStyleBlack;
    self.toolbar.translucent = YES;
    self.toolbar.barStyle = UIBarStyleBlack;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationBar.translucent = NO;
    self.navigationBar.barStyle = UIBarStyleDefault;
    self.toolbar.translucent = NO;
    self.toolbar.barStyle = UIBarStyleDefault;
}

- (void)dealloc
{
    [slideshowViewController release];
    [thumbsViewController release];
    [iPhotoGalleryDelegate release];
    [super dealloc];
}

#pragma mark Public

- (void)reloadData
{
    [thumbsViewController reloadData];
    [slideshowViewController reloadData];
}

- (void)fetchedPhotoThumb:(UIImage *)photo atIndex:(int)index
{
    [thumbsViewController fetchedPhoto:photo atIndex:index];
}

- (void)fetchedPhoto:(UIImage *)photo atIndex:(int)index
{
    [slideshowViewController fetchedPhoto:photo atIndex:index];
}

#pragma mark ThumbsViewControllerDelegate

- (int)thumbsViewControllerPhotosCount:(ThumbsViewController *)tvc
{
    return [iPhotoGalleryDelegate iPhotoGalleryPhotosCount:self];
}

- (void)thumbsViewController:(ThumbsViewController *)tvc
    fetchPhotoAtIndex:(int)index
{
    [iPhotoGalleryDelegate iPhotoGallery:self fetchPhotoThumbAtIndex:index];
}

- (void)thumbsViewController:(ThumbsViewController *)tvc
    selectedPhotoAtIndex:(int)index
{
    [slideshowViewController showPhotoAtIndex:index];
    [self pushViewController:slideshowViewController animated:YES];
}

#pragma mark SlideshowViewControllerDelegate

- (int)slideshowViewControllerPhotosCount:(SlideshowViewController *)svc
{
    return [iPhotoGalleryDelegate iPhotoGalleryPhotosCount:self];
}

- (void)slideshowViewController:(SlideshowViewController *)svc
    fetchPhotoAtIndex:(int)index
{
    [iPhotoGalleryDelegate iPhotoGallery:self fetchPhotoAtIndex:index];
}

@end
